package com.tarena.lbs.activity.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tarena.lbs.activity.api.pojo.ActivityDto;
import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.enums.ErrorCode;
import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.base.protocol.enums.ErrorCodeSupport;
import com.tarena.lbs.base.protocol.model.Result;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tarena.lbs.activity.enums.ActivityStatusEnum;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


import java.util.*;
import java.util.stream.Collectors;
import java.util.Date;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityRepository activityRepository;
    @Autowired
    private ActivityMapper activityMapper;

    @DubboReference(version = "1.0.0")
    private AttachApi attachApi;

    @Override
    public List<ActivityVO> list(Long businessId) {
        List<ActivityVO> list= activityRepository.selectByBusinessId(businessId);
        return list;
    }

    @Override
    public Result<ActivityVO> detail(Long id) {
        log.debug("活动详情业务参数:{}", id);

        return activityRepository.selectById(id);
    }

    @Override
    public void insert(ActivityAddParam param) {
        log.debug("保存活动参数:{}", param);

        if (param.getBusinessId() != null) {

            ActivityPO activityPO = new ActivityPO();
            BeanUtils.copyProperties(param, activityPO);
            activityPO.setStatus(0);
            activityPO.setCreateAt(new Date());
            activityPO.setUpdateAt(new Date());
            activityPO.setEnableStatus(0); // 启动状态
            //activityPO.setActivityJson(0);
            activityPO.setActivityType(0); // 活动类型
            activityPO.setRewardType(1);
            activityRepository.insert(activityPO);
        }else {
            throw new IllegalArgumentException("商家不存在，ID: " + param.getBusinessId());
        }
    }
    @Override
    public Result<Boolean> checkUserInActivityTarget(Long userId, Long activityId) {
        ActivityPO activity = activityRepository.findById(activityId.intValue());
        if (activity == null) {
            return Result.fail(ErrorCode.ACTIVITY_NOT_FOUND);
        }
        String targetCustomer = activity.getTargetCustomer();
        if (targetCustomer == null || targetCustomer.isEmpty()) {
            return Result.fail(ErrorCode.ACTIVITY_QUERY_FAILED);
        }
        List<String> targetUserIds = Arrays.asList(targetCustomer.split(","));
        boolean isInTarget = targetUserIds.contains(String.valueOf(userId));
        return new Result<>(isInTarget);
    }
}
